﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Geometries.Shapes
{
    /// <summary>
    /// 定义一个3x3的平面仿射变换矩阵
    /// </summary>
    public class Matrix3x3 : Matrix
    {
        public Matrix3x3() : base(3, 3)
        {
            this[0, 0] = 1; this[0, 1] = 0; this[0, 2] = 0;

            this[1, 0] = 0; this[1, 1] = 1; this[1, 2] = 0;

            this[2, 0] = 0; this[2, 1] = 0; this[2, 2] = 1;
        }

        public Matrix3x3(Matrix m) : base(3, 3)
        {
            if (m.cols != 3 || m.rows != 3)
            {
                throw new Exception("行列数不正确。");
            }
            for (int c = 0; c < this.cols; c++)
            {
                for (int r = 0; r < this.rows; r++)
                {
                    this[r, c] = m[r, c];
                }
            }

        }

        public void SetMatrix(double m11, double m12, double m21, double m22, double offsetX, double offsetY)
        {
            this[0, 0] = m11;
            this[0, 1] = m12;
            this[0, 2] = 0;

            this[1, 0] = m21;
            this[1, 1] = m22;
            this[1, 2] = 0;

            this[2, 0] = offsetX;
            this[2, 1] = offsetY;
            this[2, 2] = 1;
        }

        public double M11 { get { return this[0, 0]; } set { this[0, 0] = value; } }
        public double M12 { get { return this[0, 1]; } set { this[0, 1] = value; } }
        public double M21 { get { return this[1, 0]; } set { this[1, 0] = value; } }
        public double M22 { get { return this[1, 1]; } set { this[1, 1] = value; } }
        public double OffsetX { get { return this[2, 0]; } set { this[2, 0] = value; } }
        public double OffsetY { get { return this[2, 1]; } set { this[2, 1] = value; } }

        public static Matrix3x3 operator *(Matrix3x3 m1, Matrix3x3 m2)
        {
            return new Matrix3x3(Matrix.Multiply(m1, m2));
        }

    }
}
